﻿define(function (require, exports, module) {
    require("./TextSelectionToolHandler.js");
    var TextSelectionToolHandler = WebPDF.Tools.TextSelectionToolHandler;

    /**
     * Text selection plugin.
     * @extends {WebPDF.IReaderPlugin}
     * @param {WebPDF.ReaderApp} readerApp app instance
     * @extends {WebPDF.IReaderPlugin}
     * @constructor
     */
    WebPDF.TextSelectionPlugin = function (readerApp) {
        var _readerApp = readerApp,
            _isInitialized = false,
            _selectionToolHandler = null;
            //_self = this;

        this.getPluginName = function () {
            return "TextSelection Plugin";
        };

        this.onRegister = function () {
            if (_isInitialized)
                return true;
        };

        this.init = function () {
            _selectionToolHandler = new TextSelectionToolHandler(_readerApp);
            _readerApp.registerToolHandler(_selectionToolHandler);
            _bindEvents();
            _createCmdAndBtn();
            _isInitialized = true;
        };
        this.unload = function () {
        };
        
        function _createCmdAndBtn() {
        	_readerApp.addMenuItem({
        		name: "MenuTextCopy",
                 createHtml: function () {
                     return "<li id='idMenuTextCopy' menuname='MenuTextCopy'><span class='copy icon'>Copy</span></li>";
                 },
                 onShow: function (jqMenuItem) {

                 },
                 onSelect: function (jqMenuItem) {
                     if (jqMenuItem) {
                         if (!jqMenuItem.hasClass("disabled")) {
                             var pTextSelectTool = _selectionToolHandler.getTextSelectTool();
                             if (pTextSelectTool) {
                                 WebPDF.copyText(_readerApp,pTextSelectTool.getSelectedText());
                             }
                         }
                         else
                             return false;
                     }
                 }
             });            
            _readerApp.addMenuItem({
                name: "MenuHighlight",
                createHtml: function () {
                    return "<li class='icon' menuname='MenuHighlight'><span class='highlight icon'>Highlight</span></li>";
                },
                onShow: function (jqMenuItem) {       
                    jqMenuItem.addClass("disabled");
                },
                onSelect: function (jqMenuItem) {                      
                }
            });
             _readerApp.addMenuItem({
                name: "MenuStrikeout",
                createHtml: function () {
                    return "<li class='icon' menuname='MenuStrikeout'><span class='strikeout icon'>Strikeout</span></li>";
                },
                onShow: function (jqMenuItem) {     
                     jqMenuItem.addClass("disabled");
                },
                onSelect: function (jqMenuItem) {   
                  
                }
            });            
            _readerApp.addMenuItem({
                name: "MenuUnderline",
                createHtml: function () {
                    return "<li class='icon' menuname='MenuUnderline'><span class='underline icon'>Underline</span></li>";
                },
                onShow: function (jqMenuItem) {     
                      jqMenuItem.addClass("disabled");
                },
                onSelect: function (jqMenuItem) {                     
                }
            });
            
        }

        function _resetHighlightText(){
            var textSelectTool = _selectionToolHandler.getTextSelectTool();
            if (textSelectTool != null) {
                textSelectTool.highLightAllSelectedText();
                textSelectTool.hideSelectionOperationDiv();
            }
        }

        /**
         * Bind all events.
         * @private
         */
        function _bindEvents(){
            $(_readerApp).on(WebPDF.EventList.DOCVIEW_ZOOM_CHANGED,function(){
                _resetHighlightText();
            }).on(WebPDF.EventList.DOCVIEW_PRE_RESIZE,function(){
                _resetHighlightText();
            }).on(WebPDF.EventList.PAGE_SHOW_COMPLETE,function(event,data){
                if (data.pageView != null) {
                    var textManager = _readerApp.getTextManager();
                    if (textManager != null) {
                        textManager.ajaxGetTextPage(data.pageView.getPageIndex(), true);
                    }
                }
            });
        }
        
        
    };

    exports.createPlugin = function (readerApp) {
        return  new WebPDF.TextSelectionPlugin(readerApp);
    };
});